/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package br.ufes.cwtools.joinus.home.communities;

import java.io.StringReader;
import java.util.Vector;

import org.xml.sax.InputSource;

import br.ufes.cwtools.joinus.home.Home2;
import br.ufes.cwtools.joinus.util.ServerCom;

import android.app.Activity;
import br.ufes.cwtools.joinus.R;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class ShowCommSearch {
	
	Activity a;
	LayoutInflater viewInflate;
	ListView v;
	long[] ids;

	public ShowCommSearch(String search, Activity a) {
		this.a = a;
		((Home2)a).setPreviousScreen(Home2.COMMU);
		viewInflate = LayoutInflater.from(a);
		a.setContentView(R.layout.search_result);
		this.v = (ListView)a.findViewById(R.id.search_result_list_view);
		SetButs();
		PutIntoList(search);
	}

	private void SetButs() {
		ImageButton c =(ImageButton)a.findViewById(R.id.search_result_join_button);
		c.setOnClickListener(new OnClickListener(){
			public void onClick(View arg0) {
				for (int i = 0; i < v.getAdapter().getCount(); i++) {
					RelativeLayout r = (RelativeLayout) v.getAdapter().getView(i, null, null);
					CheckBox cb = (CheckBox) r.getChildAt(0);
					if (cb.isChecked()) {
						Log.d("DEBUG",ids[i]+"**");
						ServerCom.joinCommu(ids[i]+"");
					}
				}
			}			
		});
	}

	private void PutIntoList(String search) {
		String xml = ServerCom.searchCommu(search);
		String[][] array=null;
		if (xml != null) {
			InputSource is = new InputSource();
			is.setCharacterStream(new StringReader(xml));
			Commu factory = new Commu();
			Vector<Commu> commu = factory.fromXml(is);
			array = new String[commu.size()][];
			ids= new long[commu.size()];
			int i = 0;
			for(Commu c : commu){
				if(c.getCommuName()==null)return;
				Log.d("COMMUSEARCH",c.getCommuName());
				array[i]=new String[2];
				array[i][0]=new String(c.getCommuName());
				array[i][1]=new String(c.getOwnerName());
				ids[i]=c.getId();
				i++;
			}
			add(array);
		} else {
			Toast.makeText(a, "Server error", Toast.LENGTH_LONG).show();
		}
	}

	public void add(String[][] toAdd) {
		RelativeLayout[] rl = new RelativeLayout[toAdd.length];
		for (int i = 0; i < toAdd.length; i++) {
			RelativeLayout r = (RelativeLayout) viewInflate.inflate(R.layout.line_template, null);
			((TextView) r.getChildAt(1)).setText(toAdd[i][0]);
			((TextView) r.getChildAt(2)).setText("Community onwer: " + toAdd[i][1]);

			rl[i] = r;
		}
		v.setAdapter(new MyListAdapter(rl));
	}

	final class MyListAdapter extends BaseAdapter {
		
		RelativeLayout[] item;

		public MyListAdapter(RelativeLayout[] v) {
			item = v;
		}

		public Object getItem(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			return item[position];
		}

		public long getItemId(int position) {
			return position;
		}

		public int getCount() {
			return item.length;
		}
		
	}

}
